/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package foundation;

import entity.xml.EUtente;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

/**
 *
 * @author Quirino
 */
public class FUtenti {
    
    private static FUtenti istance=null;
    private SAXBuilder builder;
    private String urlDoc;
    private Document doc;
    private XMLOutputter outp;
    private ArrayList<EUtente> userList;
    private HashMap tempRubrica;
    
    private FUtenti(){
        this.builder= new SAXBuilder();
        this.outp= new XMLOutputter();
        this.urlDoc= "src/database/utenti.xml";
    }
    
    public static FUtenti getIstance(){
        if(istance==null) istance= new FUtenti();
        return istance;
    }
    
    public ArrayList<EUtente> getListaUtenti() throws JDOMException, IOException{
        
        this.userList= new ArrayList<EUtente>();
        this.tempRubrica= new HashMap();
        this.doc= this.builder.build(this.urlDoc);
        
        
        Element root = doc.getRootElement();
        List allUsers= root.getChildren();
        
        for(int i=0; i<allUsers.size(); i++){
            
            Element user= (Element)allUsers.get(i);
            
            EUtente euser= new EUtente();
            
            Element idUser= user.getChild("id");
            euser.setID(Integer.parseInt(idUser.getText()));
            Element usernameUser= user.getChild("username");
            euser.setUsername(usernameUser.getText());
            Element passwordUser= user.getChild("password");
            euser.setPassword(passwordUser.getText());
            Element avatarUser= user.getChild("avatar");
            euser.setAvatar(avatarUser.getText());
            Element nicknameUser= user.getChild("nickname");
            euser.setNickname(nicknameUser.getText());
            Element nomeUser= user.getChild("nome");
            euser.setNome(nomeUser.getText());
            Element cognomeUser= user.getChild("cognome");
            euser.setCognome(cognomeUser.getText());
            
            Element dataNascita= user.getChild("datanascita");
            euser.setDatanascita(FUtility.getDateByXML(dataNascita.getText()));
            
            Element indirizzoUser= user.getChild("indirizzo");
            euser.setIndirizzo(indirizzoUser.getText());
            Element cittaUser= user.getChild("citta");
            euser.setCitta(cittaUser.getText());
            
            Element rubricaUser= user.getChild("rubrica");
            ArrayList<Integer> idContattiRubrica= new ArrayList<Integer>();
            List<Element> lisRubrica= rubricaUser.getChildren();
            for(int j=0; j<lisRubrica.size(); j++){
                
                Element contattoRubrica= lisRubrica.get(j);
                idContattiRubrica.add(Integer.parseInt(contattoRubrica.getText()));
                
            }
            this.tempRubrica.put(euser, idContattiRubrica);
            
            this.userList.add(euser);
            
        }
        
        this.setRubricaUsers();
        
        return this.userList;
        
    }
    
    public void setRubricaUsers(){
        
        for(int i=0; i<this.userList.size(); i++){
            
            ArrayList<Integer> lis= new ArrayList<Integer>();
            lis= (ArrayList<Integer>) this.tempRubrica.get(this.userList.get(i));
            for(int j=0; j<lis.size(); j++){
                
                EUtente eutente= new EUtente();
                eutente= this.getUserByID(lis.get(j));
                this.userList.get(i).getRubrica().add(eutente);
            
            }
            
        }
        
    }
    
    public void printUserList(){
        
        System.out.println("=============================   LISTA DEGLI UTENTI  =============================");
        for(int i=0; i<this.userList.size(); i++){
            
            EUtente eu= (EUtente) this.userList.get(i);
            eu.printUser();
            
        }
        System.out.println("=================================================================================");
        
    }
    
    public EUtente getUserByID(int usID){
        
        EUtente eu= null;
        
        for(int i=0; i<this.userList.size(); i++){
            
            if(((EUtente)this.userList.get(i)).getID()==usID) eu= (EUtente)this.userList.get(i);
            
        }
        
        return eu;
        
    }
    
    public EUtente getUserByNickname(String usNickname){
        
        EUtente eu= null;
        
        for(int i=0; i<this.userList.size(); i++){
            
            if(((EUtente)this.userList.get(i)).getNickname().equals(usNickname)) eu= (EUtente)this.userList.get(i);
            
        }
        
        return eu;
        
    }
    
}
